class ServiceController < ApplicationController
  before_action :set_menu

  def maintain
  	@submenu = :service_maintain
    @title = '维修服务'
  	@order_maintains = OrderMaintain.where(status: 1..2)
  end

  def maintain_show
    @title = '维修服务/明细'
    @buttons.push :action => '/service/maintain', :label => "返回"
    @order_maintain = OrderMaintain.find params[:id]
    @dtime = ChronicDuration.output((Time.now-@order_maintain.created_at).round)
    @staffs = Staff.all
    @order_maintain_unfinish = @order_maintain.order_maintain_items.where(status: 0..1)  
    if params.has_key? :add
      @staff = Staff.find params[:staff]
      @order_maintain_item = OrderMaintainItem.find params[:add]
      if !@order_maintain_item.staffs.include? @staff
        StaffMaintainRelation.create(staff_id: @staff.id, order_maintain_item_id: @order_maintain_item.id)
      end
    end
    if params.has_key? :refresh
      @order_maintain_item = OrderMaintainItem.find params[:refresh]
      @order_maintain_item.staff_maintain_relations.destroy_all
    end
    if params.has_key? :start
      @order_maintain_item = OrderMaintainItem.find params[:start]
      if @order_maintain_item.staffs.blank?
        @infos.push "请增加服务人员"
      else
        @order_maintain_item.update(status: 1)
        @order_maintain_item.order_maintain.update(status: 2)
        redirect_to '/service/maintain_show?id='+params[:id]
      end
    end
    if params.has_key? :over
      @order_maintain_item = OrderMaintainItem.find params[:over]
      if @order_maintain_item.staffs.blank?
        @infos.push "请增加服务人员"
      else
        @order_maintain_item.update(status: 2)
        redirect_to '/service/maintain_show?id='+params[:id]
      end
    end
    if params.has_key? :finish
      @order_maintain.update(status: 3)
      redirect_to '/service/maintain'
    end
  end

  def beauty
  	@submenu = :service_beauty
    @title = '美容服务'
    @order_beauties = OrderBeauty.where(status: 1..2)
  end

  def beauty_show
    @title = '美容服务/明细'
    @buttons.push :action => '/service/beauty', :label => "返回"
    @order_beauty = OrderBeauty.find params[:id]
    @dtime = ChronicDuration.output((Time.now-@order_beauty.created_at).round)
    @staffs = Staff.all
    @order_beauty_unfinish = @order_beauty.order_beauty_items.where(status: 0..1)  
    if params.has_key? :add
      @staff = Staff.find params[:staff]
      @order_beauty_item = OrderBeautyItem.find params[:add]
      if !@order_beauty_item.staffs.include? @staff
        StaffBeautyRelation.create(staff_id: @staff.id, order_beauty_item_id: @order_beauty_item.id)
      end
    end
    if params.has_key? :refresh
      @order_beauty_item = OrderBeautyItem.find params[:refresh]
      @order_beauty_item.staff_beauty_relations.destroy_all
    end
    if params.has_key? :start
      @order_beauty_item = OrderBeautyItem.find params[:start]
      if @order_beauty_item.staffs.blank?
        @infos.push "请增加服务人员"
      else
        @order_beauty_item.update(status: 1)
        @order_beauty_item.order_beauty.update(status: 2)
        redirect_to '/service/beauty_show?id='+params[:id]
      end
    end
    if params.has_key? :over
      @order_beauty_item = OrderBeautyItem.find params[:over]
      if @order_beauty_item.staffs.blank?
        @infos.push "请增加服务人员"
      else
        @order_beauty_item.update(status: 2)
        redirect_to '/service/beauty_show?id='+params[:id]
      end
    end
    if params.has_key? :finish
      @order_beauty.update(status: 3)
      redirect_to '/service/beauty'
    end
  end

  private
  def set_menu
    @mainmenu = :service
  end
end
